package cn.cqie.attendance.student.sevice.impl;


import cn.cqie.attendance.student.domian.*;
import cn.cqie.attendance.student.sevice.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.cqie.attendance.student.mapper.StudentMapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


/**
 * 学生信息Service业务层处理
 * 
 * @author xhd
 * @date 2021-06-18
 */
@Service
public class StudentServiceImpl implements IStudentService
{
    @Autowired
    private StudentMapper studentMapper;

    /**
     * 通过Id查询学生信息
     * @param stuId
     * @return student
     */
    @Override
    public Student queryStudentById(Long stuId)
    {
        return studentMapper.queryStudentById(stuId);
    }

    /**
     * 学生实训小组列表查询
     * @param stuId
     * @return studentList
     */
    @Override
    public List<ResultStudent> queryStudentList(Long stuId)
    {
        List<ResultStudent> studentLists=  studentMapper.queryStudentList(stuId);
        List<ResultStudent> students = new ArrayList<>();
        String courseName =null;
        for (ResultStudent student: studentLists) {
            courseName =  student.getCourseName();
            student.setCourseHours(studentMapper.queryCourseHourse(stuId,courseName));
            String selfHourse=studentMapper.querySelfHourse(student.getStuId(),courseName);
            if(selfHourse==null){
                student.setSelfHours(0);
            }else{
                student.setSelfHours(Float.parseFloat(selfHourse));
            }
            students.add(student);
        }
        return students;
    }

    /**
     * 学生实训成绩列表查询
     * @param stuId
     * @return groupScoreList
     */
    @Override
    public  List<ResultScore>  queryStudentGroupItemScore(Long stuId){
        List<ResultScore> scoreList= studentMapper.queryStudentGroupItemScore(stuId);
        List<ResultScore> scoreList1 = new ArrayList<>();
        for (ResultScore resultScore : scoreList) {
            Long courseId=resultScore.getCourseId();
          List<Student> studentList = studentMapper.queryStudentGroupItemScoreRankNo(courseId);
            for (Student student : studentList) {
                if(student.getCourseGroup().getGroupName().equals(resultScore.getGroupName())){
                    resultScore.setRankNo(student.getRankNo());
                    break;
                }
            }
            scoreList1.add(resultScore);
        }
        return scoreList1;
    }

    /**
     * 学生实训小组成绩列表排行查询
     * @param courseId
     * @return groupScoreList
     */
    @Override
    public List<Student> queryStudentGroupItemScoreRankNo(Long courseId){

        List<Student> studentList =  studentMapper.queryStudentGroupItemScoreRankNo(courseId);
        return studentList;
    }

    /**
     * @param stuId 学生id
     * @param email 学生邮箱
     * @return 返回修改结果 1成功 0失败
     * 修改学生邮箱
     */
    @Override
    public int updateEmail(Long stuId, String email){
        return studentMapper.updateEmail(stuId,email);
    }

    /**
     * @param stuId 学生id
     * @param phone 学生电话号码
     * 修改学生电话号码
     */
    @Override
    public int updatePhoneNumber(Long stuId, String phone){
        return studentMapper.updatePhone(stuId,phone);
    }

    /**
     * 学生上传头像
     * @param filePath
     * @param stuId
     * */
    @Override
    public boolean uploadStudentFace(Long stuId, String filePath){
        try {
            studentMapper.uploadStudentFace(stuId, filePath);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 条件查询学生实训课程信息
     * @param course
     */
    @Override
    public List<Course> queryStudentCourseByInfo(QueryCourse course){
        return studentMapper.queryStudentCourseByInfo(course);
    }

    /**
     * 条件查询学生实训作业信息列表
     * @param homework
     */
    @Override
    public List<ResultHomework> queryStudentTaskByInfo(QueryHomework homework){
       return studentMapper.queryStudentTaskByInfo(homework);
    }

    /**
     * 学生实训作业内容查询
     * @param
     */
    @Override
    public List<ResultHomework> queryStudentTaskInfoById(Long phId, Long homId){
        return studentMapper.queryStudentTaskInfoById(phId,homId);
    }


    @Override
    @Transactional(rollbackFor=Exception.class)
    public int insertAttachment(String filePath,int homeworkType,Long phId,Long homId){
        return studentMapper.insertAttachment(filePath,homeworkType,phId,homId);
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public int updateHomework(Long phId, Long homId, String answerContent,int status){
        return studentMapper.updateHomework(phId,homId,answerContent,status);
    }

    /**
     * 草稿内容查询
     * */
    @Override
   public ResultHomework queryDraftInfo(int status, Long phId, Long homId){
        return studentMapper.queryDraftInfo(status,phId,homId);
    }

    /**
     * 查询作业批复情况
     * @param
     * */
   @Override
   public Homework queryHomeworkApproval(Long phId,Long homId,Long stuId){
        return studentMapper.queryHomeworkApproval(phId,homId,stuId);
   }

    /**
     * 查询学生的考勤记录
     * @param stuId
     * */
   @Override
   public List<ResultAttendance> queryStudentAttendance(Long stuId){
       return studentMapper.queryStudentAttendance(stuId);
   }

    /**
     * 查询学生的考勤记录
     * @param stuId,pageNum,pageSize
     * */
    @Override
    public List<ResultAttendance> queryStudentAttendanceRecord(Long stuId){
       return studentMapper.queryStudentAttendanceRecord(stuId);
    }

    /**
     * 下拉框数据查询，通过学生id查询该学生所有的课程
     * @param stuId
     * @return courseList
     * */
    @Override
    public List<Course> queryStudentCourseById(Long stuId){
        return studentMapper.queryStudentCourseById(stuId);
    }



    /**
     * 下拉框数据查询，通过学生id查询该学生实训老师
     * @param stuId
     * @return trainteacherCourseList
     * */
    @Override
    public List<TrainteacherCourse> queryStudentTrainteacherById(Long stuId){
       return studentMapper.queryStudentTrainteacherById(stuId);
    }


    /**
     * 下拉框数据查询，通过学生id查询该学生责任老师
     * @param stuId
     * @return courseList
     * */
    @Override
    public List<Course> queryStudentResponseteacherById(Long stuId){
        return studentMapper.queryStudentResponseteacherById(stuId);
    }



}
